package com.blackandwhitesensation.sice.presentation.backingBeans;

import com.blackandwhitesensation.sice.exceptions.*;
import com.blackandwhitesensation.sice.modelo.*;
import com.blackandwhitesensation.sice.modelo.dto.TipoEventoDTO;
import com.blackandwhitesensation.sice.presentation.businessDelegate.BusinessDelegatorView;
import com.blackandwhitesensation.sice.utilities.*;

import org.primefaces.component.calendar.*;
import org.primefaces.component.commandbutton.CommandButton;
import org.primefaces.component.inputtext.InputText;

import org.primefaces.event.DateSelectEvent;
import org.primefaces.event.RowEditEvent;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.TimeZone;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;


/**
 *
 * @author Zathura Code Generator http://code.google.com/p/zathura/
 *
 */
public class TipoEventoView {
    private InputText txtNombreEvento;
    private InputText txtIdTipoEvento;
    private CommandButton btnSave;
    private CommandButton btnModify;
    private CommandButton btnDelete;
    private CommandButton btnClear;
    private List<TipoEventoDTO> data;
    private TipoEventoDTO selectedTipoEvento;

    public TipoEventoView() {
        super();
    }

    public void rowEventListener(RowEditEvent e) {
        try {
            TipoEventoDTO tipoEventoDTO = (TipoEventoDTO) e.getObject();

            if (txtNombreEvento == null) {
                txtNombreEvento = new InputText();
            }

            txtNombreEvento.setValue(tipoEventoDTO.getNombreEvento());

            if (txtIdTipoEvento == null) {
                txtIdTipoEvento = new InputText();
            }

            txtIdTipoEvento.setValue(tipoEventoDTO.getIdTipoEvento());

            action_modify();
        } catch (Exception ex) {
        }
    }

    public String action_clear() {
        if (txtNombreEvento != null) {
            txtNombreEvento.setValue(null);
            txtNombreEvento.setDisabled(true);
        }

        if (txtIdTipoEvento != null) {
            txtIdTipoEvento.setValue(null);
            txtIdTipoEvento.setDisabled(false);
        }

        if (btnSave != null) {
            btnSave.setDisabled(true);
        }

        if (btnDelete != null) {
            btnDelete.setDisabled(true);
        }

        if (btnModify != null) {
            btnModify.setDisabled(true);
        }

        if (btnClear != null) {
            btnClear.setDisabled(false);
        }

        return "";
    }

    public void listener_txtId() {
        TipoEvento entity = null;

        try {
            Long idTipoEvento = new Long(txtIdTipoEvento.getValue().toString());
            entity = BusinessDelegatorView.getTipoEvento(idTipoEvento);
        } catch (Exception e) {
            // TODO: handle exception
        }

        if (entity == null) {
            txtNombreEvento.setDisabled(false);
            txtIdTipoEvento.setDisabled(false);
            btnSave.setDisabled(false);
            btnDelete.setDisabled(true);
            btnModify.setDisabled(true);
            btnClear.setDisabled(false);
        } else {
            txtNombreEvento.setValue(entity.getNombreEvento());
            txtNombreEvento.setDisabled(false);
            txtIdTipoEvento.setValue(entity.getIdTipoEvento());
            txtIdTipoEvento.setDisabled(true);
            btnSave.setDisabled(true);
            btnDelete.setDisabled(false);
            btnModify.setDisabled(false);
            btnClear.setDisabled(false);
        }
    }

    public String action_save() {
        try {
            BusinessDelegatorView.saveTipoEvento(FacesUtils.checkLong(
                    txtIdTipoEvento), FacesUtils.checkString(txtNombreEvento));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYSAVED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_delete() {
        try {
            BusinessDelegatorView.deleteTipoEvento(FacesUtils.checkLong(
                    txtIdTipoEvento));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modify() {
        try {
            BusinessDelegatorView.updateTipoEvento(FacesUtils.checkLong(
                    txtIdTipoEvento), FacesUtils.checkString(txtNombreEvento));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
            action_clear();
        } catch (Exception e) {
            data = null;
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String actionDeleteDataTableEditable() {
        try {
            if (txtIdTipoEvento == null) {
                txtIdTipoEvento = new InputText();
            }

            txtIdTipoEvento.setValue(selectedTipoEvento.getIdTipoEvento());

            BusinessDelegatorView.deleteTipoEvento(FacesUtils.checkLong(
                    txtIdTipoEvento));
            data.remove(selectedTipoEvento);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modifyWitDTO(Long idTipoEvento, String nombreEvento)
        throws Exception {
        try {
            BusinessDelegatorView.updateTipoEvento(idTipoEvento, nombreEvento);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
        } catch (Exception e) {
            //renderManager.getOnDemandRenderer("TipoEventoView").requestRender();
            FacesUtils.addErrorMessage(e.getMessage());
            throw e;
        }

        return "";
    }

    public InputText getTxtNombreEvento() {
        return txtNombreEvento;
    }

    public void setTxtNombreEvento(InputText txtNombreEvento) {
        this.txtNombreEvento = txtNombreEvento;
    }

    public InputText getTxtIdTipoEvento() {
        return txtIdTipoEvento;
    }

    public void setTxtIdTipoEvento(InputText txtIdTipoEvento) {
        this.txtIdTipoEvento = txtIdTipoEvento;
    }

    public List<TipoEventoDTO> getData() {
        try {
            if (data == null) {
                data = BusinessDelegatorView.getDataTipoEvento();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return data;
    }

    public void setData(List<TipoEventoDTO> tipoEventoDTO) {
        this.data = tipoEventoDTO;
    }

    public TipoEventoDTO getSelectedTipoEvento() {
        return selectedTipoEvento;
    }

    public void setSelectedTipoEvento(TipoEventoDTO tipoEvento) {
        this.selectedTipoEvento = tipoEvento;
    }

    public CommandButton getBtnSave() {
        return btnSave;
    }

    public void setBtnSave(CommandButton btnSave) {
        this.btnSave = btnSave;
    }

    public CommandButton getBtnModify() {
        return btnModify;
    }

    public void setBtnModify(CommandButton btnModify) {
        this.btnModify = btnModify;
    }

    public CommandButton getBtnDelete() {
        return btnDelete;
    }

    public void setBtnDelete(CommandButton btnDelete) {
        this.btnDelete = btnDelete;
    }

    public CommandButton getBtnClear() {
        return btnClear;
    }

    public void setBtnClear(CommandButton btnClear) {
        this.btnClear = btnClear;
    }

    public TimeZone getTimeZone() {
        return java.util.TimeZone.getDefault();
    }
}
